package com.example.swaggerDemo.service;

import com.example.swaggerDemo.Dao.PushDao;
import com.example.swaggerDemo.entity.SysUserEntity;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;

@Resource
@Service
public class PushService implements PushDao {
    @Autowired
    private Environment env;

    @Autowired
    private UserService SysUserDao;

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    /*
     * 免费订单通知
     * */
    @Override
    public void push(@RequestParam("openid") String openid,@RequestParam("userid") String userid,@RequestParam("Username") String Username) {
        //1，配置

        WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
        wxStorage.setAppId(env.getProperty("wxlog.appid"));
        wxStorage.setSecret(env.getProperty("wxlog.appsecret"));
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxStorage);
        String createtime= df.format(new Date());
        //2,推送消息
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(openid)//要推送的用户openid
                .templateId("zwSiks9NaP8_lVXoSGuIwlj0IcK8ru3eioBtXSGFE88")//模版id
                .url("http://qb.kmhkj.top/view/index.html")//点击模版消息要访问的网址
                .build();
        //3,如果是正式版发送模版消息，这里需要配置你的信息

        templateMessage.addData(new WxMpTemplateData("first", "您有一个新的订单请你及时处理", "#FF00FF"));
        templateMessage.addData(new WxMpTemplateData("keyword1", Username, "#9A9A9A"));
        templateMessage.addData(new WxMpTemplateData("keyword2", "无", "#9A9A9A"));
        templateMessage.addData(new WxMpTemplateData("keyword3", "免费测斑", "#9A9A9A"));
        templateMessage.addData(new WxMpTemplateData("keyword4", createtime, "#9A9A9A"));
        try {
            wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (Exception e) {
            System.out.println("推送失败：" + e.getMessage());
            e.printStackTrace();
        }

    }


    @Override
    /**
     * 升级订单、再次服务订单推送
     */
    public void pushUporader(@RequestParam("userid") String userid, @RequestParam("type") String type) {
        //1，配置
        String str="";
        try {
            SysUserEntity dto= SysUserDao.findByid(userid);//客户信息
            SysUserEntity agentdto= SysUserDao.findByid(dto.getUpopenid());//经纪人信息
            if (type.equals("0"))
            {
                str="升级订单";
            }
            else if (type.equals("1")){
                str="再次服务订单";
            }

            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId(env.getProperty("wxlog.appid"));
            wxStorage.setSecret(env.getProperty("wxlog.appsecret"));
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            String createtime= df.format(new Date());
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(agentdto.getOpenid())//要推送的用户openid
                    .templateId("zwSiks9NaP8_lVXoSGuIwlj0IcK8ru3eioBtXSGFE88")//模版id
                    .url("http://qb.kmhkj.top/view/index.html")//点击模版消息要访问的网址
                    .build();
            //3,如果是正式版发送模版消息，这里需要配置你的信息

            templateMessage.addData(new WxMpTemplateData("first", "您有一个新的订单请你及时处理", "#FF00FF"));
            templateMessage.addData(new WxMpTemplateData("keyword1", dto.getUsername(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword2", dto.getMobile(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword3", str, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword4", createtime, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword5", dto.getAddress(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword6", "请留意预约时间，并及时为您的客户提供服务", "#9A9A9A"));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失败：" + e.getMessage());
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
    /**
     * 局部 签约 新的订单通知 用户id，类型
     */
    public void pushagent(@RequestParam("userid") String userid, @RequestParam("type") String type) {
        //1，配置
        String str="";
        try {
            SysUserEntity dto= SysUserDao.findByid(userid);//客户信息
            SysUserEntity agentdto= SysUserDao.findByid(dto.getUpopenid());//经纪人信息
            if (type.equals("0"))
            {
                str="体验订单";
            }
            else if (type.equals("1")){
                str="签约订单";
            }

            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId(env.getProperty("wxlog.appid"));
            wxStorage.setSecret(env.getProperty("wxlog.appsecret"));
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            String createtime= df.format(new Date());
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(agentdto.getOpenid())//要推送的用户openid
                    .templateId("zwSiks9NaP8_lVXoSGuIwlj0IcK8ru3eioBtXSGFE88")//模版id
                    .url("http://qb.kmhkj.top/view/index.html")//点击模版消息要访问的网址
                    .build();
            //3,如果是正式版发送模版消息，这里需要配置你的信息

            templateMessage.addData(new WxMpTemplateData("first", "您有一个新的订单请你及时处理", "#FF00FF"));
            templateMessage.addData(new WxMpTemplateData("keyword1", dto.getUsername(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword2", dto.getMobile(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword3", str, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword4", createtime, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword5", dto.getAddress(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword6", "请留意预约时间，并及时为您的客户提供服务", "#9A9A9A"));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失败：" + e.getMessage());
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    @Override
       /**
     * 局部 签约 新的订单通知 用户id，类型
     */
    public void pushagentass(@RequestParam("userid") String userid,@RequestParam("assitantid") String assitantid) {
        //1，配置
        String str="协助订单";
        String openid="";
        try {
            SysUserEntity dto= SysUserDao.findByid(userid);//客户信息
            SysUserEntity assitantdto= SysUserDao.findByid(assitantid);//客户信息
            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId(env.getProperty("wxlog.appid"));
            wxStorage.setSecret(env.getProperty("wxlog.appsecret"));
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            String createtime= df.format(new Date());
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(assitantdto.getOpenid())//要推送的用户openid
                    .templateId("zwSiks9NaP8_lVXoSGuIwlj0IcK8ru3eioBtXSGFE88")//模版id
                    .url("http://qb.kmhkj.top/view/index.html")//点击模版消息要访问的网址
                    .build();
            //3,如果是正式版发送模版消息，这里需要配置你的信息

            templateMessage.addData(new WxMpTemplateData("first", "您有一个新的协助订单请你及时处理", "#FF00FF"));
            templateMessage.addData(new WxMpTemplateData("keyword1", dto.getUsername(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword2", dto.getMobile(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword3", str, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword4", createtime, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword5", dto.getAddress(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword6", "请留意预约时间，并及时为您的客户提供服务", "#9A9A9A"));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失败：" + e.getMessage());
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

    /**
     * 局部 签约 新的订单通知 用户id，类型
     */
    @Override
    public void pushaadmingentass(@RequestParam("userid") String userid,@RequestParam("assitantid") String assitantid) {
        //1，配置
        String str="协助订单";
        String openid="";
        try {
            SysUserEntity dto= SysUserDao.findByid(userid);//客户信息

            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId(env.getProperty("wxlog.appid"));
            wxStorage.setSecret(env.getProperty("wxlog.appsecret"));
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            String createtime= df.format(new Date());
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(assitantid)//要推送的用户openid
                    .templateId("zwSiks9NaP8_lVXoSGuIwlj0IcK8ru3eioBtXSGFE88")//模版id
                    .url("http://qb.kmhkj.top/view/index.html")//点击模版消息要访问的网址
                    .build();
            //3,如果是正式版发送模版消息，这里需要配置你的信息

            templateMessage.addData(new WxMpTemplateData("first", "您有一个新的协助订单需要指派请你及时处理", "#FF00FF"));
            templateMessage.addData(new WxMpTemplateData("keyword1", dto.getUsername(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword2", dto.getMobile(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword3", str, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword4", createtime, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword5", dto.getAddress(), "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword6", "请留意预约时间，并及时为您的客户提供服务", "#9A9A9A"));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失败：" + e.getMessage());
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    @Override
    public void pushPay(@RequestParam("openid") String openid, @RequestParam("money") String money, @RequestParam("orderid") String orderid) {
        SysUserEntity dto=null;
        try {
             dto= SysUserDao.findByid(openid);//客户信息
            WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
            wxStorage.setAppId(env.getProperty("wxlog.appid"));
            wxStorage.setSecret(env.getProperty("wxlog.appsecret"));
            WxMpService wxMpService = new WxMpServiceImpl();
            wxMpService.setWxMpConfigStorage(wxStorage);
            String createtime= df.format(new Date());
            //2,推送消息
            WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                    .toUser(dto.getOpenid())//要推送的用户openid
                    .templateId("DZmTGR08Gl50IVf3IUNHNAF6hqHOwSrLVvu3lmUmnTs")//模版id
                    .url("http://qb.kmhkj.top/view/index.html")//点击模版消息要访问的网址
                    .build();
            //3,如果是正式版发送模版消息，这里需要配置你的信息
            templateMessage.addData(new WxMpTemplateData("first", "您的订单已经支付", "#FF00FF"));
            templateMessage.addData(new WxMpTemplateData("keyword1", "东晨皇玺创业平台", "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword2", money, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword3", orderid, "#9A9A9A"));
            templateMessage.addData(new WxMpTemplateData("keyword4", createtime, "#9A9A9A"));
            try {
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            } catch (Exception e) {
                System.out.println("推送失败：" + e.getMessage());
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

